package com.eazyimage.lab.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eazyimage.lab.entity.FileData;
import com.eazyimage.lab.entity.Response;
import com.eazyimage.lab.entity.parmas.DownParam;
import com.eazyimage.lab.mapper.FileDataMapper;
import com.eazyimage.lab.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;

/**
 * @author Pzi
 * @create 2022-05-03 16:04
 */
@Service
public class DataServiceImpl implements DataService {

    @Autowired
    private FileDataMapper fileDataMapper;

    @Override
    public Response findFile(String searchMsg) {

        LambdaQueryWrapper<FileData> lqw = new LambdaQueryWrapper<>();
        lqw.like(FileData::getUserName, searchMsg)   //  按照userName查找
                .or()
                .like(FileData::getFilePath, searchMsg); //  按文件名查找
        List<FileData> fileData = fileDataMapper.selectList(lqw);

        //  如果根据条件查到了数据，就将数据返回
        if (fileData != null) {
            return Response.success(fileData);
        } else {
            //  如果没查到数据，就返回空
            return Response.fail();
        }
    }

    @Override
    public Response findAllFileData() {

        List<FileData> fileData = fileDataMapper.selectList(null);
        return Response.success(fileData);

    }

    @Override
    public Response deleteFileById(Integer id) {
        LambdaQueryWrapper<FileData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FileData::getId,id);
        int delete = fileDataMapper.delete(lqw);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("删除的id", id);
        return Response.success(map);
    }

    @Override
    public void export(DownParam downParam, HttpServletResponse response) {

        String filePath = downParam.getFilePath();
        System.out.println("输出downParam里面的filePath"+filePath);

        //  首先拼接出文件的路径
//        String fileName = filePath.substring(filePath.indexOf(":") + 1, filePath.indexOf("}"));
//        System.out.println("fileName是"+fileName);
//
//        String name = fileName.substring(fileName.lastIndexOf("\\" + 1), fileName.indexOf("p") + 1);
//        System.out.println("name = "+name);

        if (filePath != null) {
            //设置文件路径
            File file = new File(filePath);
            System.out.println("打印file");
            System.out.println(file);

            //File file = new File(realPath , fileName);
            if (file.exists()) {
//                response.setContentType("application/force-download");// 设置强制下载不打开
//                response.setContentType("application/json");// 设置强制下载不打开
              response.setContentType("application/octet-stream");
                response.addHeader("Content-Disposition", "attachment;fileName=" + filePath);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("传输成功");
//                    return null;
//                    return Response.success("下载成功");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally { // 做关闭操作
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

//        return Response.fail("下载失败");
    }

    @Override
    public void exportData(String filePath, HttpServletResponse response) {

        if (filePath != null) {
            //设置文件路径
            File file = new File(filePath);
            System.out.println("打印file");
            System.out.println(file);

            //File file = new File(realPath , fileName);
            if (file.exists()) {
//                response.setContentType("application/force-download");// 设置强制下载不打开
//                response.setContentType("application/json");// 设置强制下载不打开
                response.setContentType("application/octet-stream");
                response.addHeader("Content-Disposition", "attachment;fileName=" + filePath);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("传输成功");
//                    return null;
//                    return Response.success("下载成功");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally { // 做关闭操作
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }


}
